package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class ApiXxlJob {
    private static final Logger logger = LoggerFactory.getLogger(ApiXxlJob.class);

    private final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(6, TimeUnit.HOURS)
            .writeTimeout(6, TimeUnit.HOURS)
            .readTimeout(6, TimeUnit.HOURS)
            .build();

    @Value("${apiBaseUrl}")
    private String apiBaseUrl;

    /**
     * 获取必应每日壁纸
     */
    @XxlJob("bingImgSyncTask")
    public void bingImgSyncTask() {
        XxlJobHelper.log("bingImgSyncTask.");
        String url = apiBaseUrl + "/task/v1/bingImgSyncTask";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response resp = client.newCall(request).execute()) {
            if (resp.code() != HttpStatus.OK.value()) {
                // 抛出异常
                XxlJobHelper.handleFail("uri:" + url + ", http status:" + resp.code() +
                        ", Call http service error! " + resp.message());
            }
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }

    }

    @XxlJob("getImgWallpaperscraftSyncTaskPc")
    public void getImgWallpaperscraftSyncTaskPc() {
        XxlJobHelper.log("getImgWallpaperscraftSyncTaskPc.");
        String url = apiBaseUrl + "/task/v1/getImgWallpaperscraftSyncTaskPc";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response resp = client.newCall(request).execute()) {
            if (resp.code() != HttpStatus.OK.value()) {
                // 抛出异常
                XxlJobHelper.handleFail("uri:" + url + ", http status:" + resp.code() +
                        ", Call http service error! " + resp.message());
            }
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }

    }

    @XxlJob("getImgWallpaperscraftSyncTaskMobile")
    public void getImgWallpaperscraftSyncTaskMobile() {
        XxlJobHelper.log("getImgWallpaperscraftSyncTaskMobile.");
        String url = apiBaseUrl + "/task/v1/getImgWallpaperscraftSyncTaskMobile";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response resp = client.newCall(request).execute()) {
            if (resp.code() != HttpStatus.OK.value()) {
                // 抛出异常
                XxlJobHelper.handleFail("uri:" + url + ", http status:" + resp.code() +
                        ", Call http service error! " + resp.message());
            }
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }

    }

    @XxlJob("getImgWpcoderSyncTaskMobile")
    public void getImgWpcoderSyncTaskMobile() {
        XxlJobHelper.log("getImgWpcoderSyncTaskMobile.");
        String url = apiBaseUrl + "/task/v1/getImgWpcoderSyncTaskMobile";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response resp = client.newCall(request).execute()) {
            if (resp.code() != HttpStatus.OK.value()) {
                // 抛出异常
                XxlJobHelper.handleFail("uri:" + url + ", http status:" + resp.code() +
                        ", Call http service error! " + resp.message());
            }
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }

    }


    @XxlJob("quarkDownload")
    public void quarkDownload() {
        XxlJobHelper.log("quarkDownload.");
        String url = apiBaseUrl + "/quark/v1/download";
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response resp = client.newCall(request).execute()) {
            if (resp.code() != HttpStatus.OK.value()) {
                // 抛出异常
                XxlJobHelper.handleFail("uri:" + url + ", http status:" + resp.code() +
                        ", Call http service error! " + resp.message());
            }
        } catch (Exception e) {
            XxlJobHelper.log(e);
            XxlJobHelper.handleFail(e.getMessage());
        }

    }


}
